package chenji.dailykit.ItemEditor.operationnode.nodes;

import java.util.Map;
import java.util.Set;

import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;

import com.meowj.langutils.lang.LanguageHelper;

import chenji.dailykit.Main;
import chenji.dailykit.ItemEditor.ItemEditor;
import chenji.dailykit.ItemEditor.operationnode.OperationNode;
import chenji.dailykit.ItemEditor.operationnode.SimpleItemStack;

public class EnchantmentEditor extends OperationNode {
	/**
	 * 1 - 等待附魔等级 2 - 等待删除附魔位置 3 - 等待编辑附魔位置 4 - 等待编辑附魔等级
	 */
int state;
	Enchantment temp;

	public EnchantmentEditor(ItemStack i, ItemEditor e, Player p) {
		super(i, e, p);

	}

	@Override
	public void setInventory(Inventory inv) {
		inv.setItem(43, Main.getOperationNodeItemLibrary().getBack());
		inv.setItem(28, new SimpleItemStack(Material.BOOK, new String[] { "§c查看当前物品的附魔" }, " "));
		inv.setItem(29, new SimpleItemStack(Material.BOOK, new String[] { "§c增加一个附魔" }, " "));
		inv.setItem(30, new SimpleItemStack(Material.BOOK, new String[] { "§c删除一个附魔" }, " "));
		inv.setItem(31, new SimpleItemStack(Material.BOOK, new String[] { "§c编辑一个附魔等级" }, " "));
	}

	@Override
	public void Click(int slot) {
		if (slot == 43) {
			this.getItemEditor().chengeMainMenuEditor();
			return;
		}
		if (slot == 28) {
			listEnhancement();
			return;
		}
		if (slot == 29) {
			this.getItemEditor().chengeEnchantmentAddEditor();
			return;
		}
		if (slot == 30) {
			this.state = 2;
			this.getItemEditor().getChat();
			this.getPlayer().sendMessage("请输入你要删除的附魔位置。");
			this.getPlayer().sendMessage("§a输入 getList 可获取当前的附魔列表。");
			this.getPlayer().closeInventory();
		}
		if (slot == 31) {
			this.state = 2;
			this.getItemEditor().getChat();
			this.getPlayer().sendMessage("请输入你要编辑的附魔位置。");
			this.getPlayer().sendMessage("§a输入 getList 可获取当前的附魔列表。");
			this.getPlayer().closeInventory();
		}

	}

	private void listEnhancement() {
		if (this.getItem().getItemMeta().getEnchants() == null) {
			StringBuilder ss = new StringBuilder();
			ss.append("§c=====§a物品 ");
			ss.append(this.getItem().getItemMeta().getDisplayName());
			ss.append(" §a没有附魔。§c======");
			this.getPlayer().sendMessage(ss.toString());
			this.getPlayer().sendMessage("§a输入 §c/ItemEditor openInv §a返回上一界面");
			return;
		}

		if (this.getItem().getItemMeta().getEnchants().isEmpty()) {
			StringBuilder ss = new StringBuilder();
			ss.append("§c=====§a物品 ");
			ss.append(this.getItem().getItemMeta().getDisplayName());
			ss.append(" §a没有附魔。§c======");
			this.getPlayer().sendMessage(ss.toString());
			this.getPlayer().sendMessage("§a输入 §c/ItemEditor openInv §a返回上一界面");
		}

		StringBuilder ss = new StringBuilder();
		ss.append("§c=====§a物品 ");
		ss.append(this.getItem().getItemMeta().getDisplayName());
		ss.append(" §a的附魔列表§c======");
		this.getPlayer().sendMessage(ss.toString());
		Map<Enchantment, Integer> map = this.getItem().getEnchantments();
		Set<Enchantment> m = this.getItem().getEnchantments().keySet();
		int i = 0;
		for (Enchantment ma : m) {
			StringBuilder sss = new StringBuilder();
			sss.append("§c第 §a");
			sss.append(i);
			sss.append(" §c个 - §a");
			sss.append(LanguageHelper.getEnchantmentName(ma, getPlayer()));
			sss.append(" §c等级 - §a");
			sss.append(map.get(ma));
			this.getPlayer().sendMessage(sss.toString());
			i++;
		}
		ss = new StringBuilder();
		ss.append("§c 共 §a");
		ss.append(m.size());
		ss.append(" §c个。");
		this.getPlayer().sendMessage("§a输入 §c/ItemEditor openInv §a返回上一界面");
		return;

	}

	@Override
	public void upPage() {
		// TODO Auto-generated method stub

	}

	@Override
	public void downPage() {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean setChat(String m) {
		Set<Enchantment> set = this.getItem().getEnchantments().keySet();
		if (this.state == 3) {
			int i = 0;
			try {
				i = Integer.valueOf(m);
			} catch (Exception e) {
				this.getPlayer().sendMessage("§c错误，你输入的不是一个数字。请重新输入。");
				return true;
			}
			if (i < 0 || i > set.size()) {
				this.getPlayer().sendMessage("§c错误，指定的位置不存在。请重新输入。");
				this.getPlayer().sendMessage("§c输入 getList 可获取当前的附魔列表。");
				return true;
			}

			int ia = 0;
			for (Enchantment delm : set) {
				if (ia == i) {
					this.temp = delm;
					break;
				} else {
					ia++;
				}
			}
			if (this.temp == null) {
				this.getPlayer().sendMessage("§c错误，指定的位置不存在。请重新输入。");
				this.getPlayer().sendMessage("§c输入 getList 可获取当前的附魔列表。");
				return true;
			}
			this.getPlayer().sendMessage("§c请输入你要编辑的等级");
			this.getPlayer().sendMessage("§c输入 getList 可获取当前的附魔列表。");
			this.getItemEditor().getChat();
			this.state = 4;
			return true;
		}
		if (this.state == 4) {
			int i = 0;
			try {
				i = Integer.valueOf(m);
			} catch (Exception e) {
				this.getPlayer().sendMessage("§c错误，你输入的不是一个数字。请重新输入。");
				return true;
			}
			if (i == 0) {
				this.getPlayer().sendMessage("§c错误，等级必须非零。请重新输入。");
				return true;
			}
			ItemStack item = this.getItem();
			item.addUnsafeEnchantment(this.temp, i);
			Main.getItemEditorManager().setItem(this.getPlayer(), item);
			Main.getItemEditorManager().openEditorInventory(this.getPlayer());
			this.getPlayer().sendMessage("§c修改成功！");
			this.state = 0;
			return true;
		}
		if (this.state == 2) {
			int i = 0;
			try {
				i = Integer.valueOf(m);
			} catch (Exception e) {
				this.getPlayer().sendMessage("§c错误，你输入的不是一个数字。");

				return true;
			}
			if (i < 0 || i > set.size()) {
				this.getPlayer().sendMessage("§c错误，指定的位置不存在。");
				this.getPlayer().sendMessage("§c输入 getList 可获取当前的附魔列表。");
				return true;
			}
			Enchantment del = null;
			int ia = 0;
			for (Enchantment delm : set) {
				if (ia == i) {
					del = delm;
					break;
				} else {
					ia++;
				}
			}
			if (del == null) {
				this.getPlayer().sendMessage("§c错误，指定的位置不存在。");
				this.getPlayer().sendMessage("§c输入 getList 可获取当前的附魔列表。");
				return true;
			}
			ItemStack item = this.getItem();
			item.removeEnchantment(del);
			Main.getItemEditorManager().setItem(this.getPlayer(), item);
			Main.getItemEditorManager().openEditorInventory(this.getPlayer());
			this.getPlayer().sendMessage("§c删除成功");
			this.state = 0;
			return true;
		}
		return false;
	}

}
